package com.ruoyi.chiwen.service.impl;

import com.alibaba.fastjson.JSON;
import com.ruoyi.chiwen.domain.TbBankcard;
import com.ruoyi.chiwen.domain.TbIdCard;
import com.ruoyi.chiwen.domain.TbUser;
import com.ruoyi.chiwen.mapper.TbBankcardMapper;
import com.ruoyi.chiwen.mapper.TbUserMapper;
import com.ruoyi.chiwen.service.ApiCommonService;
import com.ruoyi.chiwen.util.TencentCloudUtil;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.StringUtils;
import com.sun.org.apache.xpath.internal.operations.Bool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @program: ruoyi
 * @description:
 * @author: 佐佐木
 * @create: 2021-06-16 23:10
 **/
@Service
public class ApiCommonServiceImpl implements ApiCommonService
{
    @Autowired
    private RedisCache redisCache;

    @Autowired
    private TbUserMapper tbUserMapper;

    @Autowired
    private TbBankcardMapper tbBankcardMapper;


    @Override
    public AjaxResult getIdCode(String imgUrl) {
        if(StringUtils.isEmpty(imgUrl))return AjaxResult.error("请传入正确参数！");

        String idInfo = TencentCloudUtil.IdCard( getTCSecretId(), getTCSecretKey(),imgUrl);
//        String idInfo = "{\"Name\":\"刘洋\",\"Sex\":\"女\",\"Nation\":\"汉\",\"Birth\":\"1995/5/13\",\"Address\":\"广东省深圳市南山区腾讯大厦\",\"IdNum\":\"440305199505132561\",\"Authority\":\"\",\"ValidDate\":\"\",\"AdvancedInfo\":\"{}\",\"RequestId\":\"4f50f73e-877c-4f9b-abcf-5a7f33daff62\"}\n";
//          String idInfo = "{\"Name\":\"\",\"Sex\":\"\",\"Nation\":\"\",\"Birth\":\"\",\"Address\":\"\",\"IdNum\":\"\",\"Authority\":\"镇平县公安局\",\"ValidDate\":\"2014.07.16-2024.07.16\",\"AdvancedInfo\":\"{}\",\"RequestId\":\"1aaed8ef-9ee1-4c40-9ee9-026f2083ee72\"}\n";
        if(idInfo.equals("false")) return AjaxResult.error("未能识别身份证信息，请重试！");
         Map mapTypes = JSON.parseObject(idInfo);
        return AjaxResult.success("success",mapTypes);
    }




    @Override
    public AjaxResult getBankCard(Long userId, String imgUrl) {
        if(StringUtils.isEmpty(imgUrl)) return AjaxResult.error("请传入正确参数！");
        String idInfo = TencentCloudUtil.bankCard(getTCSecretId(), getTCSecretKey(),imgUrl);
//        String idInfo = "{\"CardNo\":\"6217876101001073338\",\"BankInfo\":\"中国银行(01040000)\",\"ValidDate\":\"\",\"CardType\":\"借记卡\",\"CardName\":\"借记IC个人普卡\",\"RequestId\":\"3fc285d3-fb84-47a2-a373-0985a26b2923\"}";
//        if(idInfo.equals("false"))return AjaxResult.error("未能识别该卡信息，请重试！");
        Map mapTypes = JSON.parseObject(idInfo);
        if (checkBankcard((String) mapTypes.get("CardNo")))AjaxResult.error("改卡已被绑定，请绑定新的卡片！");
//
////        插入一张银行卡信息
        TbBankcard tbBankcard = washToTbBankcard(mapTypes);
        tbBankcard.setUserId(userId);
        tbBankcardMapper.insertTbBankcard(tbBankcard);
        return AjaxResult.success("success",mapTypes);
    }

    @Override
    public AjaxResult getActOrder(String imgUrl) {
        return null;
    }

    @Override
    public AjaxResult saveIdCard(Long Id, TbIdCard tbIdCard) {

//        存入数据库
        TbUser tbUser = new TbUser();
        if(tbIdCard.getAuthority()==null){
            if (checkIdcard( tbIdCard.getIdNum())&&!(tbIdCard.getIdNum()).equals(""))return AjaxResult.error("该身份证已被绑定！");
            tbUser = washToTbuser(tbIdCard,"zheng",tbIdCard.getImgUrl());
        }else{ tbUser = washToTbuser(tbIdCard,"fan",tbIdCard.getImgUrl()); }

        tbUser.setUserId(Id);
        tbUserMapper.updateTbUser(tbUser);
        return AjaxResult.success("success",tbIdCard);
    }

    @Override
    public AjaxResult changeStage(Long Id, String num) {
        TbUser tbUser = new TbUser();

        tbUser.setUserId(Id);
        tbUser.setStepsNumber(Long.valueOf(num));
        tbUser.setInfoIsOk(0);
        if (num.equals("5"))tbUser.setInfoIsOk(1);
        tbUserMapper.updateTbUser(tbUser);
        return AjaxResult.success("success");
    }

    @Override
    public AjaxResult getIdCard2(String imgUrl) {
        if(StringUtils.isEmpty(imgUrl))return AjaxResult.error("请传入正确参数！");

        String idInfo = TencentCloudUtil.IdCard( getTCSecretId(), getTCSecretKey(),imgUrl);
//        String idInfo = "{\"Name\":\"刘洋\",\"Sex\":\"女\",\"Nation\":\"汉\",\"Birth\":\"1995/5/13\",\"Address\":\"广东省深圳市南山区腾讯大厦\",\"IdNum\":\"440305199505132561\",\"Authority\":\"\",\"ValidDate\":\"\",\"AdvancedInfo\":\"{}\",\"RequestId\":\"4f50f73e-877c-4f9b-abcf-5a7f33daff62\"}\n";
//        String idInfo = "{\"Name\":\"\",\"Sex\":\"\",\"Nation\":\"\",\"Birth\":\"\",\"Address\":\"\",\"IdNum\":\"\",\"Authority\":\"镇平县公安局\",\"ValidDate\":\"2014.07.16-2024.07.16\",\"AdvancedInfo\":\"{}\",\"RequestId\":\"1aaed8ef-9ee1-4c40-9ee9-026f2083ee72\"}\n";
        if(idInfo.equals("false")) return AjaxResult.error("未能识别身份证信息，请重试！");
        Map mapTypes = JSON.parseObject(idInfo);
        return AjaxResult.success("success",mapTypes);
    }


    public Boolean checkIdcard(String idBO){
        TbUser tt= new TbUser();
        tt.setIdCardNumber(idBO);
        return  tbUserMapper.selectTbUserList(tt).size()>0;
    }

    public Boolean checkBankcard(String num){
        TbBankcard tbBankcard=new TbBankcard();
        tbBankcard.setBankCardNumber(num);

        return  tbBankcardMapper.selectTbBankcardList(tbBankcard).size()>0;
    }


    public TbUser washToTbuser(TbIdCard tbIdCard,String type,String imgUrl){

        TbUser tbUser =new TbUser();
        if(type.equals("zheng")){
            tbUser.setIdCardNumber(tbIdCard.getIdNum());//  身份证号 440305199505132561
            tbUser.setUserName(tbIdCard.getName());  //  姓名  刘洋
            tbUser.setUserSex(tbIdCard.getSex()); //  性别  女
            tbUser.setNational(tbIdCard.getNation());//  民族 汉
            tbUser.setCardAddress(tbIdCard.getAddress());  //  地址 广东省深圳市南山区腾讯大厦
            tbUser.setCardStartTime(tbIdCard.getBirth()); //  生日 1995/5/13
            tbUser.setIdCardPositive(imgUrl);
        }else   if(type.equals("fan")){
            tbUser.setIssuingAuthority(tbIdCard.getAuthority()); //  发证机关
            tbUser.setCardStartTime(tbIdCard.getValidDate()); //  证件有效期
            tbUser.setIdCardReverse(imgUrl);
        }

        return tbUser;
    }

    public TbBankcard washToTbBankcard(Map mapTypes){
        TbBankcard tbBankcard =new TbBankcard();
        tbBankcard.setBankCardNumber((String) mapTypes.get("CardNo"));
        tbBankcard.setBankName((String) mapTypes.get("CardName"));
        return tbBankcard;
    }

    /**
    * @Description: 获取系统配置的 腾讯api参数
    */
    public String getTCSecretKey(){
        return Convert.toStr(redisCache.getCacheObject("sys_config:TencentCloud_SecretKey"));
    }
    public String getTCSecretId(){
        return Convert.toStr(redisCache.getCacheObject("sys_config:TencentCloud_SecretId"));
    }
    
}
